﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using ABCTile.Models;
using ABCTile.DAL;

namespace TeamC.Controllers
{
    public class ProjectController : Controller
    {
        private ABCDbContext db = new ABCDbContext();

        //
        // GET: /Project/

        public ViewResult Index()
        {
            return View(db.Projects.ToList());
        }

        //
        // GET: /Project/SearchProject

        public PartialViewResult SearchProject(string SearchString)
        {
            IEnumerable<Project> model = db.Projects.ToList();

            if (SearchString != null)
            {
                model = model.Where(m => m.projectName.ToUpper().Contains(SearchString.ToUpper())
                    || m.city.ToUpper().Contains(SearchString.ToUpper())
                    || m.state.ToUpper().Contains(SearchString.ToUpper())
                    || m.building_type.ToUpper().Contains(SearchString.ToUpper())
                    || m.work_type.ToUpper().Contains(SearchString.ToUpper())
                    || m.ProjectID.ToString() == SearchString
                    || m.zip.ToString() == SearchString
                    || m.architectureID.ToString() == SearchString
                    || m.ownerID.ToString() == SearchString
                    || m.Status.StatusName.ToUpper().Contains(SearchString.ToUpper()));
            }

            return PartialView(model);
        }

        //
        // GET: /Project/Details/5

        public ViewResult Details(int id)
        {
            Project project = db.Projects.Find(id);
            return View(project);
        }

        //
        // GET: /Project/Create

        public ActionResult Create()
        {
            return View();
        }

        //
        // POST: /Project/Create

        [HttpPost]
        public ActionResult Create(Project project)
        {
            project.StatusID = 1;
            if (ModelState.IsValid)
            {
                db.Projects.Add(project);
                db.SaveChanges();
                return RedirectToAction("Index");
            }

            return View(project);
        }

        //
        // GET: /Project/Edit/5

        public ActionResult Edit(int id)
        {
            Project project = db.Projects.Find(id);
            /*
             * SelectList() is used to create dropdown list.
             * The first field(db.Statuses) : The item of dropdown list. In this case, this method get all status items from databases
             * The second value : the data value field. In this case, the value of StatusID will be saved in project.StatusID . 
             * The third value : the data text field. In this case, the value of StatusName will be displyed in the dropdown List.
             * The fourth value : the selected value. If project.StatusID was 1, Initialized would be already selected .
             */
            ViewBag.StatusID = new SelectList(db.Statuses, "StatusID", "StatusName", project.StatusID);
            return View(project);
        }

        //
        // POST: /Project/Edit/5

        [HttpPost]
        public ActionResult Edit(Project project)
        {
            if (ModelState.IsValid)
            {
                db.Entry(project).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index");
            }

            ViewBag.StatusID = new SelectList(db.Statuses, "StatusID", "StatusName", project.StatusID);
            return View(project);
        }

        //
        // GET: /Project/Delete/5

        public ActionResult Delete(int id)
        {
            Project project = db.Projects.Find(id);
            return View(project);
        }

        //
        // POST: /Project/Delete/5

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(int id)
        {
            Project project = db.Projects.Find(id);
            db.Projects.Remove(project);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }

}